home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / Genie / Projects / Pedestal / Source / Sources / Panes / PedScrollbar.cc < prev   
Encoding:
C/C++ Source or Header  |  2000-06-24  |  4.9 KB  |  254 lines

  1. /*    ===============
  2.  *    PedScrollbar.cc
  3.  *    ===============
  4.  */
  5.  
  6. #include "PedestalDebugging.h"
  7.  
  8. #include <Fonts.h>
  9. #include <Windows.h>
  10.  
  11. #include "PedScrollbar.hh"
  12.  
  13. #include "Ped1AppProcess.hh"
  14. #include "PedApplication.hh"
  15. #include "PedView.hh"
  16. #include "PedWindow.hh"
  17.  
  18.  
  19. pascal void ScrollBarActionProc(ControlHandle inControl, short inPart);
  20.  
  21.  
  22. PedScrollbar::PedScrollbar(PedView &inSuperView, short inOrientation)
  23. : PedControl(inSuperView), mOrientation(inOrientation), mActionProc(NULL)
  24. {
  25.     // Would it be significantly more efficient to reuse UPP's?
  26.     mActionProc = NewControlActionProc(ScrollBarActionProc);
  27.     mStepUnit = mJumpUnit = 1;
  28. }
  29.  
  30. PedScrollbar::~PedScrollbar()
  31. {
  32. }
  33.  
  34. void
  35. PedScrollbar::Init()
  36. {
  37. }
  38.  
  39. void
  40. PedScrollbar::Open()
  41. {
  42.     Rect bounds;
  43.     unsigned char title[] = "\p";
  44.     
  45.     PedPane::Open();
  46.     
  47.     if (macControl) return;
  48.     
  49.     GetCalcBounds(bounds);
  50.     
  51.     macControl = ::NewControl(mSuperView.Window().Ptr(), &bounds, title, 
  52.         false, 0, 0, 0, scrollBarProc, (long)this);
  53.     ::SetControlAction(macControl, mActionProc);
  54.     mJumpUnit = (bounds.bottom - bounds.top) / 2;
  55. }
  56.  
  57. void
  58. PedScrollbar::Close()
  59. {
  60.     if (macControl) {
  61.         ::DisposeControl(macControl);
  62.         macControl = NULL;
  63.     }
  64.     PedPane::Close();
  65. }
  66.  
  67. void
  68. PedScrollbar::Activate()
  69. {
  70.     if (macControl) {
  71.         ::ShowControl(macControl);
  72.     }
  73. }
  74.  
  75. void
  76. PedScrollbar::Deactivate()
  77. {
  78.     if (macControl) {
  79.         ::HideControl(macControl);
  80.     }
  81. }
  82.  
  83.  
  84. enum {kPedMsgNone, kPedMsgScroll, kPedMsgNotifyScrolledTo};
  85.  
  86. void
  87. PedScrollbar::Scroll(short inDistance, bool inUpdate)
  88. {
  89.     short v = 0, h = 0;
  90.     if (mOrientation == kPedScrollVertical) {
  91.         v = inDistance;
  92.     } else {
  93.         h = inDistance;
  94.     }
  95.     //mSuperView.Scroll(h, v);
  96.     SPedScrollInfo info = {{v, h}, inUpdate};
  97.     mSuperView.Message(kPedMsgScroll, &info);
  98. }
  99.  
  100. void
  101. PedScrollbar::SetBounds()
  102. {
  103.     Rect bounds;
  104.     
  105.     GetCalcBounds(bounds);
  106.     SetBounds(bounds);
  107. }
  108.  
  109. void
  110. PedScrollbar::SetBounds(const Rect &inBounds)
  111. {
  112.     PedPane::SetBounds(inBounds);
  113.     if (macControl) {
  114.         ::HideControl(macControl);
  115.         ::MoveControl(macControl, inBounds.left, inBounds.top);
  116.         ::SizeControl(macControl, 
  117.             inBounds.right  - inBounds.left,
  118.             inBounds.bottom - inBounds.top );
  119.         ::ShowControl(macControl);
  120.     }
  121. }
  122.  
  123.  
  124. void
  125. PedScrollbar::GetCalcBounds(Rect &outBounds)
  126. {
  127.     Rect frame;
  128.     Point offset;
  129.     
  130.     mSuperView.GetFrame(frame);
  131.     mSuperView.GetWindowToLocalOffset(offset);
  132.     
  133.     // FIXME:  This assumes both scrollbars are present.
  134.     if (mOrientation == kPedScrollVertical) {
  135.         outBounds.top = frame.top + offset.v - 1;
  136.         outBounds.bottom = frame.bottom + offset.v - 14;
  137.         outBounds.left = frame.right + offset.h - 15;
  138.         outBounds.right = frame.right + offset.h + 1;
  139.     } else {
  140.         outBounds.left = frame.left + offset.h - 1;
  141.         outBounds.right = frame.right + offset.h - 14;
  142.         outBounds.top = frame.bottom + offset.v - 15;
  143.         outBounds.bottom = frame.bottom + offset.v + 1;
  144.     }
  145. }
  146.  
  147. void
  148. PedScrollbar::SetStepUnit(short inUnit)
  149. {
  150.     mStepUnit = inUnit;
  151. }
  152.  
  153. void
  154. PedScrollbar::SetJumpUnit(short inUnit)
  155. {
  156.     mJumpUnit = inUnit;
  157. }
  158.  
  159.  
  160. void
  161. PedScrollbar::Draw()
  162. {
  163.     WindowPtr window = mSuperView.Window().Ptr();
  164.     ::UpdateControls(window, window->visRgn);
  165. }
  166.  
  167. void
  168. PedScrollbar::DispatchNullEvent(EventRecord &inEvent)
  169. {
  170. }
  171.  
  172. void
  173. PedScrollbar::DispatchClickEvent(EventRecord &inEvent)
  174. {
  175.     if (!macControl) return;
  176.     
  177.     WindowPtr window = mSuperView.Window().Ptr();
  178.     //::SetPort(macWindow);
  179.     Point pt = inEvent.where;
  180.     ::GlobalToLocal(&pt);
  181.     ControlHandle control;
  182.     short part = ::FindControl(pt, window, &control);
  183.     short oldValue, scrollDistance;
  184.     ::ClipRect(&qd.thePort->portRect);
  185.     switch (part) {
  186.         case kControlIndicatorPart:
  187.             oldValue = ::GetControlValue(macControl);
  188.             part = ::TrackControl(macControl, pt, NULL);
  189.             if (part == kControlIndicatorPart) {
  190.                 scrollDistance = Value() - oldValue;
  191.                 if (scrollDistance != 0) {
  192.                     Scroll(scrollDistance);
  193.                 }
  194.             }
  195.             break;
  196.         case kControlUpButtonPart:
  197.         case kControlDownButtonPart:
  198.         case kControlPageUpPart:
  199.         case kControlPageDownPart:
  200.             part = ::TrackControl(macControl, pt, (ControlActionUPP)-1);
  201.             break;
  202.         default:
  203.             break;
  204.     }
  205. }
  206.  
  207. void
  208. PedScrollbar::ControlAction(short inPart)
  209. {
  210.     Rect view;
  211.     
  212.     short scrollDistance;
  213.     switch (inPart) {
  214.         case kControlUpButtonPart:
  215.         case kControlDownButtonPart:
  216.             scrollDistance = mStepUnit;
  217.             break;
  218.         case kControlPageUpPart:
  219.         case kControlPageDownPart:
  220.             scrollDistance = mJumpUnit;
  221.             break;
  222.         default:
  223.             return;
  224.             break;
  225.     }
  226.     if (inPart == kControlUpButtonPart || inPart == kControlPageUpPart) {
  227.         scrollDistance = -scrollDistance;
  228.     }
  229.     
  230.     short oldValue = Value();
  231.     short max = Maximum();
  232.     short value = oldValue + scrollDistance;
  233.     if (value < 0) {
  234.         value = 0;
  235.     } else if (value > max) {
  236.         value = max;
  237.     }
  238.     if (value != oldValue) {
  239.         SetValue(value);
  240.         scrollDistance = value - oldValue;
  241.         Scroll(scrollDistance, kPedScrollRedrawImmediately);
  242.     }
  243. }
  244.  
  245. pascal
  246. void
  247. ScrollBarActionProc(ControlHandle inControl, short inPart)
  248. {
  249.     PedScrollbar *scrollbar = (PedScrollbar *)GetControlReference(inControl);
  250.     if (scrollbar == NULL) return;
  251.     
  252.     scrollbar->ControlAction(inPart);
  253. }
  254.